
import { defineStore } from 'pinia'
import {login,getQrCode,checkScan} from '@/api/login'
import { ElMessage } from 'element-plus'

export enum ZFLoginType {
    qr = 0,
    password = 1,
}
  
interface Account {
    phone: string
    token: string,///token
    code: string,///激活码
    codeTime: string,///激活码到期时间
    roles: string,// 用户角色
    user:Userinfo,
    password:string,///密码
    appvipFlag:boolean,///是否是app会员
}
interface Userinfo{
    
    accountName:string,
    anchorFlag:string,
    avr:string,
    birthday:string,
    collectCount:number,
    dlVipEffTime:string,
    dlVipFlag:string,
    flyHandFlag:string,
    gender:string,
    id:string,
    integral:number,
    pointCount:number,
    vipFlag:string,
    vipTime:string,///vip到期时间
    anchor_flag:string,
}

interface ZFQrModel {
    url:string,///扫码登录的url 
    status:string,///二维码状态 waiting :等待扫码 scanned :已扫码 expired :二维码过期 confirmed :已确认
    message:string,///二维码状态描述
}


export const useAccountStore = defineStore('account', {

    state: () => {
        return {
            isLogin: false,
            type: ZFLoginType.qr,
            account:{} as Account,
            qr:{} as ZFQrModel,
            show:false,
            timerId: null as NodeJS.Timeout | null, ///定时器id
        }
    },
    getters: {
      isAuth: (state) => state.account.appvipFlag,  
    },
    actions: {
        ////切换登录方式
        changeType() {
            this.type = this.type == 0 ? 1 : 0;
            if(this.type == ZFLoginType.qr){ ///如果是二维码登录就刷新一下token
                this.getQrCode();
            }else{
                this.closeTimer();
            }
        },
        onLogin(){
            var params = {
                phone: this.account.phone,
                password: this.account.password,
            };
            login(params).then((res:any) => {
                if(res.code == 200){
                    console.log("this.account",res.data)
                    this.isLogin = true;
                    this.account = res.data;
                    this.show = false;
                    ElMessage({
                        message: "登录成功",
                        type: 'success', 
                    })
                }else{
                    ElMessage({
                        message: "登录失败",
                        type: 'error', 
                    })
                }
            }).catch(err=>{
                console.log("err",err)
                ElMessage({
                    message: "登录失败",
                    type: 'error', 
                })
            })
          },
          
          logout(){
            this.isLogin = false;
            this.account = {} as Account;
          },

          ////获取二维码
          async getQrCode(){
             var res = await getQrCode() as any;
             console.log("getQrCode",res)
                if(res.code == 200){
                    this.closeTimer()
                    this.qr.url = res.data.token + '-web_token';
                    this.checkQrCode(res.data.token);///轮询检查二维码是否扫码
                }
          },
          ////检查二维码是否扫码 轮型二维码
          checkQrCode(token:string = ''){
             this.timerId = setInterval(async() => {
                var res = await checkScan(token) as any;
                console.log("checkQrCode1232312",res)
                if(res.code == 200){
                    this.qr.status = res.data.status;
                    this.qr.message = res.message;
                    ///二维码状态 waiting :等待扫码 scanned :已扫码 expired :二维码过期 confirmed :已确认
                    if (res.data.status == "confirmed") {
                        this.account = res.data;
                        this.isLogin = true;
                        this.closeTimer();
                        this.show = false;
                       
                    }else if(res.data.status == 'expired'){
                        this.closeTimer();
                    }
                }
             },1200) as NodeJS.Timeout;
          },

          //// 关闭定时器
          closeTimer(){
            if(this.timerId!= null){
                clearInterval(this.timerId);///清除定时器
            }
          }

          
    },
    persist: true,
      
})
